package com.beta.support;

import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * Created by vector on 2017/1/6.
 */
public abstract class ResourceImagePathBuilder {

    public static String pathPrefix = "";

    public static String userIdCardPicPath(Integer userId, String idCardPicture) {
        return buildPath("user", "id_card_picture", userId, idCardPicture);
    }

    public static String categoryPosterPath(Integer categoryId, String poster) {
        return buildPath("category", "poster", categoryId, poster);
    }

    public static String productPosterPath(Integer productId, String poster) {
        return buildPath("product", "poster", productId, poster);
    }

    public static String assetImagePath(Integer assetId, String image) {
        return buildPath("image", "attachment", assetId, image);
    }

    public static String productSlideImagePath(Integer productItemId, Integer slideIndex, String sliderImage) {
        return buildPath("product_item", "slide_img_path" + slideIndex, productItemId, sliderImage);
    }

    public static String homeRecommendPicPath(Integer homeRecommendId, String pic) {
        return buildPath("home_recommend", "pic_url", homeRecommendId, pic);
    }

    public static String homeBannerPicPath(Integer homeBannerId, String pic) {
        return buildPath("home_banner", "pic_url", homeBannerId, pic);
    }

    public static String buildPath(String resource, String property, Object resourceId, String propertyValue) {
        if (Objects.isNull(resourceId) || StringUtils.isBlank(propertyValue)) {
            return null;
        }
        String relativePath = String.join("/", resource, property, Objects.toString(resourceId), propertyValue);
        if (StringUtils.isNotBlank(pathPrefix)) {
            return String.join("/", pathPrefix.trim(), relativePath);
        }
        return relativePath;
    }

    public static void setPathPrefix(String pathPrefix) {
        ResourceImagePathBuilder.pathPrefix = pathPrefix;
    }

}
